package com.tamtamy.jtta.data.xml;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.tamtamy.jtta.data.CommentList;
import com.tamtamy.jtta.exception.NoResultException;
import com.tamtamy.jtta.exception.ParsingException;

public class XMLCommentList extends CommentList {
	
	private static final long serialVersionUID = 7503041207528039870L;
	
	private static final String KCLCOMMENT = "it.reply.tt.rest.entity.KcLComment";

	public XMLCommentList(Document xmlDocument) throws NoResultException, ParsingException {
		super();
		NodeList commentNodeList = xmlDocument.getElementsByTagName(KCLCOMMENT);
		if (commentNodeList == null || commentNodeList.getLength()==0){
			throw new NoResultException("No " + KCLCOMMENT + " element found");
		}
		for (int i = 0; i < commentNodeList.getLength(); i++) {
			Node commentNode = commentNodeList.item(i);
			try {
				Document commentDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
				commentDocument.appendChild(commentDocument.importNode(commentNode, true));
				this.add(new XMLComment(commentDocument)); 
			} catch (ParserConfigurationException parserConfigurationException) {
				throw new ParsingException("Error parsing " + KCLCOMMENT + " element : " + parserConfigurationException.getMessage());
			}  
		}		
	}

}
